/**
 * Created by lihao on 2017/5/2.
 */
import $script from 'scriptjs';
import '../main';
import '../../css/parts/common.less';
import '../../css/login.less';
var ApiService = require('../../js/module/ApiService');
var UserService = require('../../js/module/UserService');

$script.ready('jqueryFile', function() {
	$script(['//cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js'],function () {
		// import avalon from 'avalon2';
		// require('lodash')
		jQuery.validator.addMethod('password', function(value, element) {
			var reg1 = /^[^\u4e00-\u9fa5]{5,16}$/;
			var reg3 = /\d/;
			return this.optional(element) || (/*reg3.test(value) &&*/ reg1.test(value));
		}, $.validator.format("密码应为5-16位，并允许使用特殊符号(不能为纯字母组合)"));

		$('form').validate({
			errorClass: 'error',
			errorElement: 'em',
			onkeyup: false,
			ignore: '.notCheck',
			errorPlacement: function (er, el) {
				el.parent('.u-log-btn ').append(er);
			},
			rules: {
				userName: {
					required: true
				},
				password: {
					required: true
				},
				captcha: {
					required: true
				}
			},
			messages: {
				userName: {
					required: '请输入邮箱/手机号'
				},
				password: {
					required: '请输入密码'
				},
				captcha: {
					required: '请输入图片验证码'
				}
			},
			submitHandler:function (from) {
				var params = ApiService.serialize($(from).find('input[name],select[name]'));
				UserService.login(params).catch(function (err) {
					$('.imgCode').attr("src", "/common/captcha/?date=" + new Date().getTime());
				});

			}
		});
		$('#submit-a').click(function () {
			//提交表单
			$('form').submit();
		});
		
		$('.imgCode').click(function() {
			$(this).attr("src", "/common/captcha/?date=" + new Date().getTime());
			$("#captcha").val('').focus();
		});

		$(document).keydown(function(event){
			if(event.keyCode ==13){
				$("#submit-a").trigger("click");
			}
		});
	});
});

